# coding=utf-8
import reversion
from django.db import models
from gym.snippets import MultiSelectField
from core.models import NaturalPerson, Company
from core.validators import validate_only_white_spaces
from django.utils.translation import ugettext_lazy as _

WEEK_DAYS = (('0', _('Sunday')), ('1', _('Monday')), ('2', _('Tuesday')), ('3', _('Wednesday')),
             ('4', _('Thursday')), ('5', _('Friday')), ('6', _('Saturday')))

YES_NO_CHOICES = (('0', '-----'), ('1', _('No')), ('2', _('Yes')))


class Activity(models.Model):
    name = models.CharField(max_length=150, validators=[validate_only_white_spaces], verbose_name=_('Name'))
    description = models.TextField(validators=[validate_only_white_spaces], verbose_name=_('Description'))
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_('Price'), )
    active = models.BooleanField(default=True, verbose_name=_('Active'))
    gym = models.ForeignKey(Company, verbose_name=_('Gym'))

    class Meta:
        verbose_name = _('Activity')
        verbose_name_plural = _('Activities')

    def __unicode__(self):
        return self.name


class Schedule(models.Model):
    activity = models.ForeignKey(Activity, verbose_name=_('Activity'))
    start_time = models.TimeField(verbose_name=_('Start'))
    end_time = models.TimeField(verbose_name=_('End'))
    weekdays = MultiSelectField(choices=WEEK_DAYS, max_length=100, verbose_name=_('Weekdays'))

    class Meta:
        verbose_name = _('Schedule')
        verbose_name_plural = _('Schedules')

    def __unicode__(self):
        return "%s de %s á %s" % (self.weekdays, self.start_time, self.end_time)


class Employee(models.Model):
    registration = models.CharField(max_length=150, validators=[validate_only_white_spaces], verbose_name=_('Registration'))
    person = models.OneToOneField(NaturalPerson, verbose_name=_('Natural Person'))

    class Meta:
        verbose_name = _('Employee')
        verbose_name_plural = _('Employees')

    def __unicode__(self):
        return self.person.name


class Teacher(models.Model):
    employee = models.OneToOneField(Employee, null=True, blank=True, verbose_name=_('Employee'))
    activities = models.ManyToManyField(Activity, verbose_name=_('Activities'))

    class Meta:
        verbose_name = _('Teacher')
        verbose_name_plural = _('Teachers')

    def __unicode__(self):
        return self.employee.person.name


class MuscleGroup(models.Model):
    name = models.CharField(max_length=150, validators=[validate_only_white_spaces], verbose_name=_('Name'))
    active = models.BooleanField(default=True, verbose_name=_('Active'))

    class Meta:
        verbose_name = _('Muscle Group')
        verbose_name_plural = _('Muscle Groups')

    def __unicode__(self):
        return self.name


class Exercise(models.Model):
    name = models.CharField(max_length=150, validators=[validate_only_white_spaces], verbose_name=_('Name'))
    muscle_group = models.ForeignKey(MuscleGroup, verbose_name=_('Muscle Group'))

    class Meta:
        verbose_name = _('Exercise')
        verbose_name_plural = _('Exercises')

    def __unicode__(self):
        return self.name


class WorkoutSheet(models.Model):
    observation = models.TextField(blank=True, null=True, verbose_name=_('Observation'))
    responsible = models.ForeignKey(Teacher, verbose_name=_('Responsible'))
    expiration_date = models.DateField(verbose_name=_('Change Date'))

    class Meta:
        verbose_name = _('Workout Sheet')
        verbose_name_plural = _('Workout Sheets')


class ExerciseItem(models.Model):
    series = models.IntegerField(verbose_name=_('Series'))
    repetition = models.IntegerField(verbose_name=_('Repetition'))
    load = models.IntegerField(verbose_name=_('Load'), null=True, blank=True)
    interval = models.IntegerField(verbose_name=_('Interval'), null=True, blank=True)
    workout_sheet = models.ForeignKey(WorkoutSheet, verbose_name=_('Workout Sheet'))
    exercise = models.ForeignKey(Exercise, verbose_name=_('Exercise'))

    class Meta:
        verbose_name = _('Exercise Item')
        verbose_name_plural = _('Exercise Items')

    def __unicode__(self):
        return "%s de %s" % (self.serie, self.repetition)


class Question(models.Model):
    name = models.CharField(max_length=250, verbose_name=_('Name'))

    class Meta:
        verbose_name = _('Question')
        verbose_name_plural = _('Questions')

    def __unicode__(self):
        return self.name


class RegistrationFormModel(models.Model):
    name = models.CharField(max_length=150, validators=[validate_only_white_spaces], verbose_name=_('Name'))
    active = models.BooleanField(default=True, verbose_name=_('Active'))
    gym = models.ForeignKey(Company, verbose_name=_('Gym'))
    questions = models.ManyToManyField(Question,verbose_name=_('Questions'))

    class Meta:
        verbose_name = _('Registration Form Model')
        verbose_name_plural = _('Registrations Form Model')

    def __unicode__(self):
        return str(self.id)


class RegistrationForm(models.Model):

    OBJECTIVES_CHOICES = (('0', _('Emaciation')), ('1', _('Gain Muscle Mass')), ('2', _('Muscle Definition')),
                          ('3', _('Health')), ('4', _('Application to the sport practiced')))

    weight = models.FloatField(verbose_name=_('Weight'))
    height = models.FloatField(verbose_name=_('Height'))
    activities = models.ManyToManyField(Activity, verbose_name=_('Activities'))
    objective = models.CharField(max_length=2, choices=OBJECTIVES_CHOICES, verbose_name=_('Objective'))
    other_objective = models.CharField(max_length=100, verbose_name=_('Other Objective'), null=True, blank=True)
    observation = models.TextField(validators=[validate_only_white_spaces], verbose_name=_('Observation'))
    questions = models.ManyToManyField(Question,verbose_name=_('Questions'))
    model = models.ForeignKey(RegistrationFormModel, verbose_name=_('Model'))

    class Meta:
        verbose_name = _('Registration Form')
        verbose_name_plural = _('Registration Forms')

    def __unicode__(self):
        return "%s %s" % (self.objective, self.activities)


class Student(models.Model):
    person = models.OneToOneField(NaturalPerson, verbose_name=_('Natural Person'))
    registration_form = models.OneToOneField(RegistrationForm, verbose_name=_('Registration Form'))
    workout_sheet = models.OneToOneField(WorkoutSheet, verbose_name=_('Workout Sheet'))

    class Meta:
        verbose_name = _('Student')
        verbose_name_plural = _('Students')


class Answer(models.Model):
    choice = models.CharField(max_length=2, choices=YES_NO_CHOICES, verbose_name=_('Choices'))
    observation = models.TextField(blank=True, null=True, verbose_name=_('Observation'))
    question = models.ForeignKey(Question, verbose_name=_('Question'))
    student = models.ForeignKey(Student, verbose_name=_('Student'))

    class Meta:
        verbose_name = _('Answer')
        verbose_name_plural = _('Answers')

    def __unicode__(self):
        return "%s %s" % (self.question, self.choice)


reversion.register(Activity)
reversion.register(Schedule)
reversion.register(Employee)
reversion.register(Teacher)
reversion.register(MuscleGroup)
reversion.register(Exercise)
reversion.register(WorkoutSheet)
reversion.register(ExerciseItem)
reversion.register(Question)
reversion.register(RegistrationFormModel)
reversion.register(RegistrationForm)
reversion.register(Student)
reversion.register(Answer)